using UnityEngine;
using System.Collections;

public class Touch_Mode : MonoBehaviour  {
	
	private GameManager GameManager_script;
	Vector3 StickOriginPos;
	Vector3 touchPoint;
	RaycastHit Ray_hit;
	float CurrentTime;
	bool IsBeginTouch;
	void Start () {
		GameManager_script = GameManager.getInstance();
		IsBeginTouch = false;
	}
	
	void Update () 
	{
		if( Input.touchCount != 0 )
		{
			Ray ray = Camera.main.ScreenPointToRay( Input.touches[0].position );
			
			
			if( Physics.Raycast( ray , out Ray_hit ) )
			{
				if( IsBeginTouch.Equals(false) && Ray_hit.transform.tag.Equals("Player") )
				{
					IsBeginTouch = true;
					GameManager_script.Player_script.TryDash();
					return;
				}
				else if( Ray_hit.transform.tag.Equals("ground") )
				{
					IsBeginTouch = true;
					if(Vector3.Distance(Ray_hit.point,GameManager_script.Player_Object.transform.position) < 0.1f)
					{
						TouchOff();
						return;
					}
					touchPoint = Ray_hit.point - GameManager_script.Player_Object.transform.position;
					touchPoint.Normalize();
						
					
					TouchOn();
					
					return;
				}
				
				if(Vector3.Distance(Ray_hit.point,GameManager_script.Player_Object.transform.position) < 0.1f)
				{
					TouchOff();
				}
				return;
			}
		}
		TouchOff();
		IsBeginTouch = false;
	}
	void TouchOn()
	{
		IsBeginTouch = true;
		GameManager_script.Player_script.Player_Direction = touchPoint;
	}
	void TouchOff()
	{
		GameManager_script.Player_script.Player_Direction = Vector3.zero;
	}
	public Vector3 GetPos()
	{
		return Ray_hit.point;
	}
}
